package nz.ac.massey.spidernetpn.database;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.collections15.Factory;
import org.xml.sax.SAXException;

import com.sun.xml.internal.stream.writers.XMLWriter;

import nz.ac.massey.spidernetpn.graph.*;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.io.GraphFile;
import edu.uci.ics.jung.io.GraphMLReader;
import edu.uci.ics.jung.io.GraphMLWriter;

public class GraphDatabase implements GraphDatabaseInterface  {

	String workingDirectory ="";
	
	public GraphDatabase(String dir){
	
		workingDirectory = dir;
		
		
	}
	
	
	public void setGraphsLocation(String dir){
		workingDirectory = dir;
	}
	
	public List getListOfStoredGraphs() {
		
		List listOfGraphs = new ArrayList<String>();
		
		FileReader fr = null;
	
		File dir = new File(workingDirectory);

		FilenameFilter only = new OnlyExt("xml"); 
		String[] children = dir.list(only); 
		
	    if (children == null) {
	        // Either dir does not exist or is not a directory
	    } else {
	        for (int i=0; i<children.length; i++) {
	            // Get filename of file or directory
	            String filename = children[i];
	           // System.out.println("File "+i+" "+filename);
	            listOfGraphs.add(filename);
	        }
	    }
			
		return listOfGraphs;
	
	}

	
	public boolean saveGraph(String name, DirectedGraph<URL, Integer> graph){
		
		GraphMLWriter<URL, Integer> gWriter = new GraphMLWriter<URL, Integer>();
		
		Writer out = null;
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(workingDirectory+name+".xml");
			out = new OutputStreamWriter(fos, "UTF8");
			gWriter.save(graph, out);
			out.close();
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
	
		return true;
	}
		
	public DirectedGraph<URL, Integer> loadCustomGraph(String name)
			throws IOException {
		System.out.println("LCG trying to load from "+workingDirectory);
		GraphLoadXML load = new GraphLoadXML(workingDirectory);
		
		//Should throw expception
		return load.loadGraph(name);
	}

	private static class OnlyExt implements FilenameFilter { 
		String ext; 
		public OnlyExt(String ext) { 
				this.ext = "." + ext; 
			} 
			public boolean accept(File dir, String name) { 
				return name.endsWith(ext); 
			} 
		}
	
}
